/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kylingroup.JavaFX.TreeView;

import java.awt.Desktop;
import java.lang.Thread.State;
import java.lang.reflect.InvocationTargetException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.MenuItem;
import javafx.scene.effect.Effect;
import javafx.scene.effect.MotionBlur;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;

/**
 *
 * @author kylin
 */
public class UploadListCell extends ListCell<UploadListItem>{
    private ContextMenu popupMenu=null;
    private ListView listview;
    private boolean isPaused=false;
    private Thread thread=null;
    private LocalFileTransferTask task;
    private ImageView btnstart=null;
    private ImageView btncancle=null;
    
    public UploadListCell(ListView listview){
        this.listview=listview;
        popupMenu=new ContextMenu();
        initPopupMenu();
        
    }
    
    private void initPopupMenu(){
        MenuItem itemUnselect=new MenuItem("unselect");
        MenuItem itemStart=new MenuItem("start");
        popupMenu.getItems().addAll(itemStart,itemUnselect);
        
        itemStart.setOnAction(new EventHandler(){
            @Override
            public void handle(Event event){
                //new Thread(UploadListCell.this.getItem().getTransfertask()).start();
                System.out.println("status:"+thread.getState().toString());
            }
        });
        
        itemUnselect.setOnAction(new EventHandler(){
            @Override
            public void handle(Event event){
                //
                System.out.println(UploadListCell.this.getItem().getBox().getUserData());
            }
        });
    }
    
    @Override
    public void updateItem(UploadListItem item,boolean empty){
        super.updateItem(item, empty);
        if(empty){
            this.setText(null);
            this.setGraphic(null);
        }else{
            setContextMenu(popupMenu);
            setGraphic(item.getBox());
            task=UploadListCell.this.getItem().getTransfertask();
            thread=new Thread(task);
            bindAction(item);
        }
    }
    
    public void bindAction(UploadListItem item){
        MotionBlur motionBlur = new MotionBlur();
        motionBlur.setRadius(3.2);
        motionBlur.setAngle(92.1);
        
        btnstart=item.getBtnunstart();
        
        setImageButton(btnstart,"startTransfer", motionBlur);
        
        btncancle=item.getBtncancle();
        setImageButton(btncancle,"stopTransfer", motionBlur);
    }
    
    public void startTransfer() throws InterruptedException{
        if(!isPaused){
            if(task.status==0){
                thread.start();
            }else if(task.status==-1){
                task.resume();
            }
            btnstart.setImage(new Image(UploadListCell.this.getClass().getResourceAsStream("pause.png")));
            isPaused=true;
        }else{
            task.suspend();
            btnstart.setImage(new Image(UploadListCell.this.getClass().getResourceAsStream("start.png")));
            isPaused=false;
        }
    }
    
    public void stopTransfer(){
        System.out.println("cancle!");
        if(task.status==0||task.status==1){
            task.suspend();
        }
        if(task.status==-1){
            task.resume();
        }
        task.stop();
        listview.getItems().remove(UploadListCell.this.getItem());
    }
    
    public void setImageButton(ImageView btn,String method,Effect effect){
        btn.setOnMouseClicked(new EventHandler<MouseEvent>(){
            @Override
            public void handle(MouseEvent e) {
                if(e.getButton()==MouseButton.PRIMARY){
                try {
                    UploadListCell.this.getClass().getDeclaredMethod(method,null).invoke(UploadListCell.this,null);
                } catch (IllegalAccessException ex) {
                    Logger.getLogger(LocalFlatView.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IllegalArgumentException ex) {
                    Logger.getLogger(LocalFlatView.class.getName()).log(Level.SEVERE, null, ex);
                } catch (InvocationTargetException ex) {
                    Logger.getLogger(LocalFlatView.class.getName()).log(Level.SEVERE, null, ex);
                } catch (NoSuchMethodException ex) {
                    Logger.getLogger(LocalFlatView.class.getName()).log(Level.SEVERE, null, ex);
                } catch (SecurityException ex) {
                    Logger.getLogger(LocalFlatView.class.getName()).log(Level.SEVERE, null, ex);
                }
                }
            }
        });
        if(effect!=null){
            btn.setOnMouseEntered(new EventHandler<MouseEvent>(){
                @Override
                public void handle(MouseEvent event) {
                    btn.setEffect(effect);
                }
            });
            btn.setOnMouseExited(new EventHandler<MouseEvent>(){
                @Override
                public void handle(MouseEvent event) {
                    btn.setEffect(null);
                }
            });
        }
    }
}
